package org.ix.dynamicapis.service.impl;

import org.ix.dynamicapis.exception.DmpDataNotFoundException;
import org.ix.dynamicapis.model.PageList;
import org.ix.dynamicapis.utils.ObjectUtils;
import org.ix.dynamicapis.constant.DeletedConstant;
import org.ix.dynamicapis.presentation.condition.ApiMultiConfigSelectCondition;
import org.ix.dynamicapis.presentation.condition.ApiMultiConfigUpdateCondition;
import org.ix.dynamicapis.presentation.entity.ApiMultiConfigEntity;
import org.ix.dynamicapis.presentation.entity.ApiMultiConfigEntityExt;
import org.ix.dynamicapis.presentation.mapper.ApiMultiConfigMapper;
import org.ix.dynamicapis.model.request.ApiMultiConfigAddRequest;
import org.ix.dynamicapis.model.request.ApiMultiConfigPatchRequest;
import org.ix.dynamicapis.model.request.ApiMultiConfigQueryRequest;
import org.ix.dynamicapis.model.request.ApiMultiConfigSaveRequest;
import org.ix.dynamicapis.model.response.ApiMultiConfigDetailResponse;
import org.ix.dynamicapis.model.response.ApiMultiConfigListItemResponse;
import org.ix.dynamicapis.service.ApiMultiConfigService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;

/**
 * 多主键测试业务逻辑
 *
 * @author 郗世豪（rarexixi@gmail.com）
 */
@Service("apiMultiConfigService")
@Transactional
public class ApiMultiConfigServiceImpl implements ApiMultiConfigService {

    final ApiMultiConfigMapper apiMultiConfigMapper;

    @Autowired
    public ApiMultiConfigServiceImpl(ApiMultiConfigMapper apiMultiConfigMapper) {
        this.apiMultiConfigMapper = apiMultiConfigMapper;
    }

    /**
     * 添加多主键测试
     *
     * @param addRequest 多主键测试
     * @return 受影响的行数
     * @author 郗世豪（rarexixi@gmail.com）
     */
    @Override
    public ApiMultiConfigDetailResponse add(ApiMultiConfigAddRequest addRequest) {
        ApiMultiConfigEntity entity = ObjectUtils.copy(addRequest, ApiMultiConfigEntity.class);
        apiMultiConfigMapper.insert(entity);
        return getByConfigKeyAndConfigType(entity.getConfigKey(), entity.getConfigType());
    }

    /**
     * 批量添加多主键测试
     *
     * @param list 多主键测试列表
     * @return 受影响的行数
     * @author 郗世豪（rarexixi@gmail.com）
     */
    @Override
    public int batchAdd(Collection<ApiMultiConfigAddRequest> list) {
        List<ApiMultiConfigEntity> entityList = ObjectUtils.copy(list, ApiMultiConfigEntity.class);
        return apiMultiConfigMapper.batchInsert(entityList);
    }

    /**
     * 删除多主键测试
     *
     * @param patchRequest 更新条件请求
     * @return 受影响的行数
     * @author 郗世豪（rarexixi@gmail.com）
     */
    @Override
    public int delete(ApiMultiConfigPatchRequest patchRequest) {
        ApiMultiConfigUpdateCondition condition = ObjectUtils.copy(patchRequest, ApiMultiConfigUpdateCondition.class);
        return apiMultiConfigMapper.delete(condition);
    }

    /**
     * 根据ConfigKey, ConfigType更新多主键测试
     *
     * @param saveRequest 保存多主键测试请求实体
     * @param configKey ConfigKey
     * @param configType ConfigType
     * @return 更新后的多主键测试详情
     * @author 郗世豪（rarexixi@gmail.com）
     */
    @Override
    public ApiMultiConfigDetailResponse updateByConfigKeyAndConfigType(ApiMultiConfigSaveRequest saveRequest, String configKey, Integer configType) {
        ApiMultiConfigUpdateCondition condition = new ApiMultiConfigUpdateCondition();
        condition.setConfigKey(configKey);
        condition.setConfigType(configType);
        ApiMultiConfigEntity entity = ObjectUtils.copy(saveRequest, ApiMultiConfigEntity.class);
        apiMultiConfigMapper.update(entity, condition);
        ApiMultiConfigDetailResponse result;
        if (saveRequest.getConfigKey() == null || saveRequest.getConfigType() == null) {
            result = getByConfigKeyAndConfigType(configKey, configType);
        } else {
            result = getByConfigKeyAndConfigType(saveRequest.getConfigKey(), saveRequest.getConfigType());
        }
        return result;
    }

    /**
     * 根据ConfigKey, ConfigType获取多主键测试详情
     *
     * @param configKey ConfigKey
     * @param configType ConfigType
     * @return 多主键测试详情
     * @author 郗世豪（rarexixi@gmail.com）
     */
    @Override
    @Transactional(readOnly = true)
    public ApiMultiConfigDetailResponse getByConfigKeyAndConfigType(String configKey, Integer configType) {
        ApiMultiConfigEntityExt entity = apiMultiConfigMapper.detail(configKey, configType);
        if (entity == null) {
            throw new DmpDataNotFoundException("多主键测试不存在");
        }
        return ObjectUtils.copy(entity, ApiMultiConfigDetailResponse.class);
    }

    /**
     * 获取多主键测试列表
     *
     * @param queryRequest 搜索条件
     * @return 符合条件的多主键测试列表
     */
    @Override
    @Transactional(readOnly = true)
    public List<ApiMultiConfigListItemResponse> getList(ApiMultiConfigQueryRequest queryRequest) {
        ApiMultiConfigSelectCondition condition = ObjectUtils.copy(queryRequest, ApiMultiConfigSelectCondition.class);
        List<ApiMultiConfigEntityExt> list = apiMultiConfigMapper.select(condition);
        return ObjectUtils.copy(list, ApiMultiConfigListItemResponse.class);
    }

    /**
     * 分页获取多主键测试列表
     *
     * @param queryRequest 搜索条件
     * @param pageNum      页码
     * @param pageSize     分页大小
     * @return 符合条件的多主键测试分页列表
     */
    @Override
    @Transactional(readOnly = true)
    public PageList<ApiMultiConfigListItemResponse> getPageList(ApiMultiConfigQueryRequest queryRequest, Integer pageNum, Integer pageSize) {

        ApiMultiConfigSelectCondition condition = ObjectUtils.copy(queryRequest, ApiMultiConfigSelectCondition.class);
        PageInfo<ApiMultiConfigEntityExt> pageInfo = PageHelper.startPage(pageNum, pageSize).doSelectPageInfo(() -> apiMultiConfigMapper.select(condition));

        List<ApiMultiConfigListItemResponse> list = ObjectUtils.copy(pageInfo.getList(), ApiMultiConfigListItemResponse.class);
        return new PageList<>(pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal(), list);
    }
}
